# git log --pretty=format:" "
# 
# 控制显示的记录格式，常用的格式占位符写法及其代表的意义如下：
# 选项 说明
# %H   提交对象（commit）的完整哈希字串
# %h    提交对象的简短哈希字串
# %T    树对象（tree）的完整哈希字串
# %t    树对象的简短哈希字串
# %P    父对象（parent）的完整哈希字串
# %p    父对象的简短哈希字串
# %an   作者（author）的名字
# %ae   作者的电子邮件地址
# %ad   作者修订日期（可以用 -date= 选项定制格式）
# %ar   作者修订日期，按多久以前的方式显示
# %cn   提交者(committer)的名字
# %ce   提交者的电子邮件地址
# %cd   提交日期
# %cr   提交日期，按多久以前的方式显示
# %s    提交说明

cmake_minimum_required(VERSION 3.5)
project(edgeserver LANGUAGES "ASM" "C" "CXX")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

if(CMAKE_BUILD_TYPE STREQUAL "Release")
    set(CMAKE_BUILD_TYPE "Release")
else()
    set(CMAKE_BUILD_TYPE "Debug")
endif()

option(WITH_BUILD_PUBLISH "" OFF)
option(WITH_TEST "" ON)

# version info
if(WITH_BUILD_PUBLISH)
    string(TIMESTAMP vDateTime "%Y-%m-%d_%H:%M:%S")
    #vProjectDir
    execute_process(
            COMMAND basename ${CMAKE_CURRENT_SOURCE_DIR}
            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
            OUTPUT_VARIABLE vProjectDir
            OUTPUT_STRIP_TRAILING_WHITESPACE
    )

    #vGitBranch
    execute_process(
            COMMAND git rev-parse --abbrev-ref HEAD
            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
            OUTPUT_VARIABLE vGitBranch
            OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    #vGitCommit
    execute_process(
            COMMAND git log -1 --pretty=format:%h
            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
            OUTPUT_VARIABLE vGitCommit
            OUTPUT_STRIP_TRAILING_WHITESPACE
    )

    set(vFileName "${CMAKE_CURRENT_SOURCE_DIR}/src/version.h")
    file(WRITE  ${vFileName} "#ifndef _JF_VERSION_H\r\n#define _JF_VERSION_H\r\n" )
    file(APPEND ${vFileName} "const char* version=\"${vDateTime}:${vProjectDir}:${vGitBranch}:${vGitCommit}\";\r\n")
    file(APPEND ${vFileName} "#endif")
endif()

# set build output
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/BuildOutput/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/BuildOutput/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/BuildOutput/bin)
link_directories(${CMAKE_BINARY_DIR}/BuildOutput/lib)

# set cxx_flags
add_definitions(-std=c++11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wshadow -Wpointer-arith -Wundef -Wredundant-decls")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -fPIC")
set(CMAKE_CXX_FLAGS "-DSQLITE_HAS_CODEC")

# Debug
#set(CMAKE_C_FLAGS_DEBUG  "${CMAKE_C_FLAGS_DEBUG} -Wall -O0 -g -ggdb3")
set(CMAKE_C_FLAGS_DEBUG  "${CMAKE_C_FLAGS_DEBUG} -O0 -g -ggdb3")
set(CMAKE_CXX_FLAGS_DEBUG  "${CMAKE_CXX_FLAGS_DEBUG} ")

# Release
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -s ")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s ")

set(FLATBUFFER_EXECUTABLE flatc)
file(GLOB CONF_FBS "${CMAKE_CURRENT_SOURCE_DIR}/src/flat/*.fbs")
foreach(FBS ${CONF_FBS})
    message("fbs file: ${FBS}")
    execute_process(
            COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} flatc --cpp ${FBS}
            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/flat
    )
endforeach()

file(GLOB SERVER_PROTOS "${PROJECT_SOURCE_DIR}/src/server/proto/*.proto")
foreach(PROTO ${SERVER_PROTOS})
    message("proto file: ${PROTO}")
    execute_process(
            COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --cpp_out=./ --proto_path=${PROJECT_SOURCE_DIR}/src/server/proto ${PROTO}
            WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/src/server/proto
    )
endforeach()

file(GLOB_RECURSE CORE_SRCS 
	${PROJECT_SOURCE_DIR}/src*.cpp
	)

# find opencv2.4.9
find_package(OpenCV 4.1.1 REQUIRED)

find_package(PkgConfig REQUIRED)
pkg_check_modules(ffmpeg REQUIRED IMPORTED_TARGET libavcodec libavformat libavutil)
target_link_libraries(${PROJECT_NAME} PRIVATE PkgConfig::ffmpeg)

################################################################
set(ENV{PATH} /home/martink)
if(NOT DEFINED ENV{JAVA_HOME})
    # 没有找到JAVA_HOME环境变量
    message(FATAL_ERROR "not defined environment variable:JAVA_HOME")
endif()
#不能用if(ENV{JAVA_HOME})形式来判断是否定义
#但可以用if($ENV{JAVA_HOME})
################################################################
